package com.iengine.services.api.command.music;

import com.google.gson.Gson;
import com.iengine.services.api.utils.Constant;
import com.iengine.services.api.utils.OperationName;
import com.ingine.data.pojo.HReply;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class CommandMusicType extends AbstractMusicPhoneApiCommand{
	
	public static final String FIELD_SINGERS = "Singers";
	public static final String FIELD_SONGS = "Songs";
	public static final String FIELD_ALBUM = "Album";
	public static final String FIELD_SONGTYPE = "SongType";
	public static final String FIELD_MOVIE_TYPE = "MovieType";
	public static final String FIELD_PLAY_MODE = "PlayMode";
	public static final String FIELD_PAGE_NUMBER="pageNumber";
	public static final String FIELD_PAGE_SIZE="pageSize";

	/**
	 *  Singers[String]:歌手[#歌手[#歌手]]
	 *	Songs[String]:歌曲[#歌曲[#歌曲]]
	 *	Album[String]:专辑名[#专辑名[#专辑名]]
	 *	SongType[String]:歌曲类型[#歌曲类型[#歌曲类型]]
	 *	MovieType[String]:影视剧名(针对播放影视剧中的歌曲)
	 *	PlayMode[String]:单曲循环，顺序播放，随机播放，循环播放
	 */
	@Override
	protected Map formatArgs(Map map, Map nlpParams) {
		HReply reply = getRemoteData(map, nlpParams);
		String playMode=getValue(nlpParams.get(FIELD_PLAY_MODE),null);
		map=this.getFormatResult(reply,map,playMode );
		return map;
	}
	
	protected Map getFormatResult(HReply reply,Map map,String playMode){
		Map<String,Object> resultMap =new HashMap<String,Object>();
		Map  listMap=new HashMap<String,Object>();
		if(StringUtils.isBlank(playMode)){
			listMap.put(Constant.OPERATION_NAME,OperationName.sequence.name());
		}else{
			if(StringUtils.equals(OperationName.singleLoop.toString(), playMode)){
				listMap.put(Constant.OPERATION_NAME,OperationName.singleLoop.name());
			}else if(StringUtils.equals(OperationName.sequence.toString(), playMode)){
				listMap.put(Constant.OPERATION_NAME,OperationName.sequence.name());
			}else if(StringUtils.equals(OperationName.random.toString(), playMode)){
				listMap.put(Constant.OPERATION_NAME,OperationName.random.name());
			}else{
				listMap.put(Constant.OPERATION_NAME,OperationName.listLoop.name());
			}
		}
		listMap.put(Constant.MUSIC_INFO, reply.getContent());
		resultMap.put(Constant.COMMAND_NAME, Constant.COMMAND_MEDIA_OPERATION);
		resultMap.put(Constant.DATA, listMap);
		map.put(Constant.COMMAND_DATA, resultMap);
		return map;
	}
	
	public String processQuery(Map map,Map nlpParams){
		Map temp = new HashMap<String, Object>();
		temp.putAll(nlpParams);
    	Object dataSize = map.get(Constant.FIELD_DATASIZE);
    	if(null != dataSize){
    		temp.putAll((Map)dataSize);
    	}
		String singers = getValue(nlpParams.get(FIELD_SINGERS),null);
		String songers = getValue(nlpParams.get(FIELD_SONGS),null);
		String album = getValue(nlpParams.get(FIELD_ALBUM),null);
		String songType = getValue(nlpParams.get(FIELD_SONGTYPE),null);
		String pageNumber = getValue(nlpParams.get(FIELD_PAGE_NUMBER),null);
		String pageSize = getValue(nlpParams.get(FIELD_PAGE_SIZE),null);
	    Map parameterMap=new HashMap();
	    if(StringUtils.isNoneBlank(pageNumber) && StringUtils.isNoneBlank(pageSize)){
	    	parameterMap.put(CommandMusicType.FIELD_PAGE_NUMBER, pageNumber);
	    	parameterMap.put(CommandMusicType.FIELD_PAGE_SIZE, pageSize);
	    }
		if(StringUtils.isNoneBlank(singers) && StringUtils.isNoneBlank(songers) && StringUtils.isNoneBlank(album) && StringUtils.isNotBlank(songType)){
			parameterMap.put(CommandMusicType.FIELD_SINGERS, singers);
			parameterMap.put(CommandMusicType.FIELD_SONGS, songers);
			parameterMap.put(CommandMusicType.FIELD_ALBUM, album);
			parameterMap.put(CommandMusicType.FIELD_SONGTYPE, songType);
		}else if(StringUtils.isNoneBlank(singers) && StringUtils.isNoneBlank(songers) && StringUtils.isNoneBlank(album)){
			parameterMap.put(CommandMusicType.FIELD_SINGERS, singers);
			parameterMap.put(CommandMusicType.FIELD_SONGS, songers);
			parameterMap.put(CommandMusicType.FIELD_ALBUM, album);
		}else if(StringUtils.isNoneBlank(singers) && StringUtils.isNoneBlank(songers)){
			parameterMap.put(CommandMusicType.FIELD_SINGERS, singers);
			parameterMap.put(CommandMusicType.FIELD_SONGS, songers);
		}else if(StringUtils.isNoneBlank(singers)){
			parameterMap.put(CommandMusicType.FIELD_SONGS, songers);
		}else{

		}
		parameterMap.put(Constant.DATA_TYPE_FIELD,Constant.MUSIC_FIELD);
		parameterMap.put(Constant.PARAMS_DATA_FIELD, temp);
		return new Gson().toJson(parameterMap);
	}
	
	@Override
	public String getTTs() {
		return "正在为您加载";
	}
}
